package steps;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;


import com.metodo_ahp.usuario.aplicacionmetodoahp.R;

import java.util.ArrayList;

import gestores.GestorModeloAHP;
import libreria.WizardStep;
import modelo.Item;

/**
 * Created by Usuario on 26/09/2014.
 */
public class step_3_def_alternativas extends WizardStep implements View.OnClickListener, View.OnKeyListener{

    private Button btnNuevaAlternativa;
    private ViewGroup layout;
    private ScrollView scvAlternativas;


    GestorModeloAHP gestorModelo;

    public step_3_def_alternativas() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gestorModelo = (GestorModeloAHP) getActivity().getApplicationContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.step_3_lista_def_alternativa, container, false);
    }




    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        layout = (ViewGroup) this.getView().findViewById(R.id.contAlternativas);

        btnNuevaAlternativa = (Button) this.getView().findViewById(R.id.btnNuevaAlternativa);
        btnNuevaAlternativa.setOnClickListener(this);
        scvAlternativas = (ScrollView) this.getView().findViewById(R.id.scvAlternativas);

        if(gestorModelo.getAlternativas().size() > 0){
            cargarAlternativas();
            addChild();
        }else{
            this.addChild();
        }
    }


    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnNuevaAlternativa) {
            int idAlternativa = gestorModelo.getSizeMatrizBase(false);
            EditText text = (EditText) this.getView().findViewById(idAlternativa);

            String nombreAlternativa = String.valueOf(((EditText) text).getText());
            gestorModelo.addItem(nombreAlternativa, false);

            this.addChild();
        }
    }



    private void addChild()
    {
        LayoutInflater inflater = LayoutInflater.from(this.getActivity());
        int id = R.layout.step_3_item_def_alternativa;

        RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(id, null, false);
        EditText txtNombre = (EditText) relativeLayout.findViewById(R.id.txtDefAlternativaNombre);
        txtNombre.setHint("Alternativa");
        //ImageButton imgAceptar = (ImageButton) relativeLayout.findViewById(R.id.btnNuevaAlternativa);
        txtNombre.setId(gestorModelo.getSizeMatrizBase(false));

        txtNombre.setOnKeyListener(this);

        //layout params
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 2;
        relativeLayout.setPadding(2, 3, 5, 2);
        relativeLayout.setLayoutParams(params);
        ///////

        layout.addView(relativeLayout);
        layout.requestFocus();



        //scroll to last element
        //http://stackoverflow.com/questions/6438061/can-i-scroll-a-scrollview-programmatically-in-android
        scvAlternativas.post(new Runnable() {
            public void run() {
                scvAlternativas.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
        ///////

    }

    private void cargarAlternativas()
    {
        for(Item it : gestorModelo.getAlternativas()){

            LayoutInflater inflater = LayoutInflater.from(this.getActivity());
            int id = R.layout.step_3_item_def_alternativa;

            RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(id, null, false);
            EditText txtNombre = (EditText) relativeLayout.findViewById(R.id.txtDefAlternativaNombre);
            txtNombre.setHint(it.getNombre());
            txtNombre.setId(it.getId());

            txtNombre.setOnKeyListener(this);

            //layout params
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.topMargin = 2;
            relativeLayout.setPadding(2, 3, 5, 2);
            relativeLayout.setLayoutParams(params);
            ///////

            layout.addView(relativeLayout);
        }
    }

    /**
     * Called when a hardware key is dispatched to a view. This allows listeners to
     * get a chance to respond before the target view.
     * <p>Key presses in software keyboards will generally NOT trigger this method,
     * although some may elect to do so in some situations. Do not assume a
     * software input method has to be key-based; even if it is, it may use key presses
     * in a different way than you expect, so there is no way to reliably catch soft
     * input key presses.
     *
     * @param v       The view the key has been dispatched to.
     * @param keyCode The code for the physical key that was pressed
     * @param event   The KeyEvent object containing full information about
     *                the event.
     * @return True if the listener has consumed the event, false otherwise.
     */
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        return false;
    }


}
